﻿using UnityEngine;
using System.Collections;
using Dict.Blo;
using Dict.DataModel;
using ProtoBuf.Message;

namespace UI
{
    public class UIMineItem
    {
        public TableMineSkill mineData;

        public long num;


        public UIMineItem(TableMineSkill data)
        {
            mineData = data;
            num = 0;
        }

        public void InitData(SimpleItem data)
        {
            mineData = DictMineBlo.GetTableMineSkill(data.id);
            num = data.num;
        }

        public int GetCanMakeNum()
        {
            if (mineData.Type != MineItemType.MAGICGEM)
            {
                return 0;
            }

            long minNum = -1;
            string[] needs = mineData.Material.Split(';');
            for (int i = 0; i < needs.Length; i++)
            {
                string[] info = needs[i].Split(',');
                if (info[0].StartsWith(Dict.Config.TableConst.MineItem))
                {
                    long count = StaticData.playerData.GetMineItemCount(info[0]);
                    long num = count / int.Parse(info[1]);
                    if (minNum == -1 || num < minNum)
                    {
                        minNum = num;
                    }
                }
                else if (info[0] == Dict.Config.TableConst.coin)
                {
                    long num = StaticData.playerData.coin / int.Parse(info[1]);
                    if (minNum == -1 || num < minNum)
                    {
                        minNum = num;
                    }
                }
            }

            return (int)minNum;
        }

        public string GetMineItemDesc()
        {
            string desc = "";

            if (mineData.Type == MineItemType.MAGICGEM)
            {
                string propstr = "";
                string[] props = mineData.Value.Split(';');
                for (int i = 0; i < props.Length; i++)
                {
                    string[] propinfo = props[i].Split(',');
                    if (propstr != "")
                        propstr += ";";
                    int propindex = BaseAttributeData.GetIndexByDictPropId(propinfo[0]);
                    string tail = "";
                    if (propindex >= 4)
                        tail = "%";
                    propstr += TableTextBlo.GetText("word_" + propinfo[0]) + "+" + propinfo[1] + tail;
                }

                desc = propstr;
            }
            else
            {
                desc = TableTextBlo.GetText("desc_" + mineData.MineItemID);
            }

            return desc;
        }
    }
}

